package com.sllp.C71S3TsllpShop.web.back;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.SmallType;
import com.sllp.C71S3TsllpShop.biz.impl.BigTypeBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.SmallTypeBizImpl;

@Controller
public class BackClassinficationAction {

	@Resource
	private BigTypeBizImpl btbi;
	@Resource
	private SmallTypeBizImpl stbi;
	
	@GetMapping("back/toClassificiation.do")
	public String classificationBody(Model m) {
		m.addAttribute("bigTypeList", btbi.findAll());
		return "back/classification::classificationBody";
	}
	
	@PostMapping("back/toShowSmallTypeTable.do")
	public String toShowSmallTypeTable(SmallType smallType,Model m) {
		m.addAttribute("smallTypeList", stbi.findByParent(smallType.getParent()));
		return "back/classification::smallTypeShowTable";
	}
	
	@PostMapping("back/changeBigTypeState.do")
	public String changeBigType(BigType bigType , Model m) {
		btbi.updateBigTypeState(bigType);
		m.addAttribute("bigTypeList", btbi.findAll());
		return "back/classification::bigTypeShowTable";
	}
	
	@PostMapping("back/changeSmallTypeState.do")
	public String changeSmallTypeState(SmallType smallType,Model m) {
		if(smallType.getState()==2) {
			smallType.setState(1);
		}else {
			smallType.setState(2);
		}
		stbi.updateSmallType(smallType);
		m.addAttribute("smallTypeList", stbi.findByParent(smallType.getParent()));
		return "back/classification::smallTypeShowTable";
	}
	
	@ResponseBody
	@PostMapping("back/addBigTypeList.do")
	public Result addBigTypeList(@RequestBody List<String> bigTypeList) {
		System.out.println(bigTypeList);
		return btbi.addBigType(bigTypeList);
	}
	
	@ResponseBody
	@PostMapping("back/addSmallTypeList.do")
	public Result addSmallTypeList(@RequestBody List<List<String>> smallTypeList) {
		System.out.println(smallTypeList);
		List<String> smallTypeList1 = smallTypeList.get(0);
		int parent = Integer.valueOf(smallTypeList.get(1).get(0));
		return stbi.addSmallType(smallTypeList1, parent);
	}
	
	@GetMapping("back/reloadBigType.do")
	public String reloadBigType(Model m) {
		m.addAttribute("bigTypeList", btbi.findAll());
		return "back/classification::bigTypeShowTable";
	}
	
	@GetMapping("back/loadBigType.do")
	public String loadBigType(Model m) {
		m.addAttribute("bigTypeList", btbi.findAll());
		return "back/classification::addSmallTypeBigTypeId";
	}
}
